<%# The license inside this block applies to this file.
  # Copyright 2017 Google Inc.
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
-%>
<% if property.custom_flatten -%>
<%= lines(compile_template(pwd + '/' + property.custom_flatten,
                           prefix: prefix,
                           property: property,
                           pwd: pwd)) -%>
<% else -%>
<% if tf_types.include?(property.class) -%>
func flatten<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d *schema.ResourceData, config *Config) interface{} {
<% if property.is_a?(Api::Type::NestedObject) -%>
  if v == nil {
    return nil
  }
  original := v.(map[string]interface{})
  <% unless property.allow_empty_object -%>
  if len(original) == 0 {
    return nil
  }
  <% end -%>
  transformed := make(map[string]interface{})
  <% property.properties.each do |prop| -%>
    <% if prop.flatten_object -%>
    if <%= prop.api_name -%> := flatten<%= prefix -%><%= titlelize_property(property) -%><%= titlelize_property(prop) -%>(original["<%= prop.api_name -%>"], d, config); <%= prop.api_name -%> != nil {
      obj := <%= prop.api_name -%>.([]interface{})[0]
      for k, v := range obj.(map[string]interface{}) {
        transformed[k] = v
      }
    }
    <% else -%>
    transformed["<%= prop.name.underscore -%>"] =
    flatten<%= prefix -%><%= titlelize_property(property) -%><%= titlelize_property(prop) -%>(original["<%= prop.api_name -%>"], d, config)
    <% end -%>
  <% end -%>
  return []interface{}{transformed}
<% elsif property.is_a?(Api::Type::Array) && property.item_type.is_a?(Api::Type::NestedObject) -%>
  if v == nil {
    return v
  }
  l := v.([]interface{})
<%   if property.is_set -%>
<%     if property.set_hash_func.nil? -%>
  transformed := schema.NewSet(schema.HashResource(<%= namespace_property_from_object(property, object) -%>Schema()), []interface{}{})
<%     else -%>
  transformed := schema.NewSet(<%= property.set_hash_func -%>, []interface{}{})
<%     end -%>
<%   else -%>
  transformed := make([]interface{}, 0, len(l))
<%   end -%>
  for _, raw := range l {
    original := raw.(map[string]interface{})
    if len(original) < 1 {
      // Do not include empty json objects coming back from the api
      continue
    }
<%   if property.is_set -%>
    transformed.Add(map[string]interface{}{
<%   else -%>
    transformed = append(transformed, map[string]interface{}{
<%   end -%>
    <% property.item_type.properties.reject(&:ignore_read).each do |prop| -%>
      "<%= prop.name.underscore -%>": flatten<%= prefix -%><%= titlelize_property(property) -%><%= titlelize_property(prop) -%>(original["<%= prop.api_name -%>"], d, config),
    <% end -%>
    })
  }
  return transformed
<% elsif property.is_a?(Api::Type::Map) -%>
  if v == nil {
    return v
  }
  l := v.(map[string]interface{})
  transformed := make([]interface{}, 0, len(l))
  for k, raw := range l {
    original := raw.(map[string]interface{})
    transformed = append(transformed, map[string]interface{}{
      "<%= property.key_name -%>": k,
    <% property.value_type.properties.each do |prop| -%>
      "<%= Google::StringUtils.underscore(prop.name) -%>": flatten<%= prefix -%><%= titlelize_property(property) -%><%= titlelize_property(prop) -%>(original["<%= prop.api_name -%>"], d, config),
    <% end -%>
    })
  }
  return transformed
<% elsif property.is_a?(Api::Type::Integer) -%>
	// Handles the string fixed64 format
	if strVal, ok := v.(string); ok {
		if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
			return intVal
		}
	}

	// number values are represented as float64
	if floatVal, ok := v.(float64); ok {
		intVal := int(floatVal)
		return intVal
	}

	return v // let terraform core handle it otherwise
<% elsif property.is_a?(Api::Type::Array) && property.item_type.is_a?(Api::Type::ResourceRef) -%>
  if v == nil {
    return v
  }
  return convertAndMapStringArr(v.([]interface{}), ConvertSelfLinkToV1)
<% elsif property.is_a?(Api::Type::ResourceRef) -%>
  if v == nil {
    return v
  }
  return ConvertSelfLinkToV1(v.(string))
<% elsif property.is_set # item_type won't be NestedObject -%>
  if v == nil {
    return v
  }
  <% if !property.set_hash_func.nil? -%>
  return schema.NewSet(<%= property.set_hash_func -%>, v.([]interface{}))
  <% elsif property.item_type.is_a?(String) or property.item_type.is_a?(Api::Type::Enum) -%>
  return schema.NewSet(schema.HashString, v.([]interface{}))
  <% else raise 'Unknown hash function for property #{property.name}' -%>
  <% end -%>
<% else -%>
  return v
<% end # property.is_a?(Api::Type::NestedObject) -%>
}
<% if property.nested_properties? -%>
  <% property.nested_properties.each do |prop| -%>
    <%= lines(build_flatten_method(prefix + titlelize_property(property), prop, object, pwd), 1) -%>
  <% end -%>
<% end -%>
<% else -%>
  // TODO: Property '<%= property.name -%>' of type <%= property.class -%> is not supported
<% end # tf_types.include?(property.class) -%>
<% end # custom code check -%>
